//
//  EasyTakePicPresentChooserViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/28.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EasyTakePicPresentChooserViewController.h"

#import "MHNavigationController.h"
#import "EasyTakePicPublishViewController.h"

#import <FXBlurView/FXBlurView.h>

@interface EasyTakePicPresentChooserViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) EasyTakePicPresentChooserViewModel* viewModel;

// UI
//@property (weak, nonatomic) UILabel *addressLabel;

//IBOutlet
@property (weak, nonatomic) IBOutlet UIView* blurPView;
@property (weak, nonatomic) IBOutlet UIButton* cameraBtn;
@property (weak, nonatomic) IBOutlet UIButton* photoBtn;
@property (weak, nonatomic) IBOutlet UIButton* closeBtn;

@end

@implementation EasyTakePicPresentChooserViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    NSLog(@"viewDidLayoutSubviews");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
    @weakify(self);
    
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        NSLog(@"viewDidAppear");
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
        
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
    
    [[[[viewDidLayoutSVSignal take:1] zipWith:[willAppearSignal replay]] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
    [[self.cameraBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        
        @weakify(self);
        [self dismissViewControllerAnimated:YES completion:^{
            
            @strongify(self);
            EasyTakePicPublishViewModel* vm = [[EasyTakePicPublishViewModel alloc] initWithParams:@{SUViewModelTitleKey:@"生态行动（随手拍）"}];
            EasyTakePicPublishViewController* vc = [[EasyTakePicPublishViewController alloc] initWithViewModel:vm];
            
            MHNavigationController* navc = [[MHNavigationController alloc] initWithRootViewController:vc];
            
            [[UIViewController getCurrentVC] presentViewController:navc animated:YES completion:^{
                
            }];
        }];
    }];
    
    [[self.photoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self);
        
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
    
    [[self.closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       @strongify(self);
        
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }];
}

- (void)_setupAllView
{
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    [visualEffectView setFrame:self.view.bounds];
    [self.blurPView addSubview:visualEffectView];
    
//    FXBlurView *blurView = [[FXBlurView alloc] init];
//    [blurView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//    [blurView setBackgroundColor:[UIColor whiteColor]];
//    //设置模式
//    blurView.dynamic = YES;
//    //设置模糊半径
//    blurView.blurRadius = 2.0;
//
//    [self.view addSubview:blurView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
